import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        String[] s = new String[n];
        String s1 = in.next();
        String s2 = in.next();
        for (int i = 0; i< n; i++) s[i] = in.next();
        Map<String,Integer> hash = new HashMap<>();
        int ret = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            if (s[i].equals(s1)) {
                Integer t = hash.get(s2);
                if (t != null) {
                    ret = Math.min(ret,i - t);
                }
                hash.put(s1,i);
            }else if (s[i].equals(s2)) {
                Integer t = hash.get(s1);
                if (t != null) {
                    ret = Math.min(ret,i - t);
                }
                hash.put(s2,i);
            }
        }
        System.out.println(ret == Integer.MAX_VALUE ? -1 : ret);
    }
}
